package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Appointment;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.qo.MyAJAX;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.MyEnum.AppointmentEnum;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IBusinessService businessService;


    @RequiredPermission(name="预约页面",expression="appointment:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") AppointmentQueryObject qo){
        model.addAttribute("businesses",businessService.listAll());
        PageInfo<Appointment> pageInfo = appointmentService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "appointment/list";
    }


    @RequiredPermission(name="预约删除",expression="appointment:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           appointmentService.delete(id);
        }
        return "redirect:/appointment/list";
    }


    @RequiredPermission(name="预约新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment) {

        if (appointment.getId() == null) {
            //通过客服进行添加预约,直接就是履行中预约
            appointment.setStatus(AppointmentEnum.PERFORM.getValue());
            appointmentService.save(appointment);
        } else {
            appointmentService.update(appointment);
        }
        return "redirect:/appointment/list";
    }
    //客户页面预约
    @RequestMapping("/save")
    @ResponseBody
    public MyAJAX save(Appointment appointment) {
        appointment.setStatus(AppointmentEnum.PEND.getValue());
        appointmentService.save(appointment);
        return new MyAJAX();
    }


    @RequiredPermission(name="预约新增/编辑",expression="appointment:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("appointment",appointmentService.get(id));
        }
        return "appointment/input";
    }


    @RequiredPermission(name="更改预约状态",expression="appointment:updateStatus")
    @RequestMapping("/updateStatus")
    public String updateStatus(Long id, Integer status){
        appointmentService.updateStatus(id,status);
        return "redirect:/appointment/list";
    }

    @RequestMapping("/foundConsumption")
    public String foundConsumption(Long id, Integer status,String ano){
        //需要改变预约状态
        appointmentService.updateStatus(id,status);
        //通过预约信息生成一个消费单
        appointmentService.foundConsumption(ano);
        return "redirect:/consumption/input?ano="+ano;
    }
}
